<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle" style="display: flex;">
        <!-- 搜索 -->
        <label class="el-form-item-label">盘点单号</label>
        <el-input v-model.trim="query.inventoryCheckCode" clearable placeholder="盘点单号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">盘点类型</label>
        <el-select v-model="query.inventoryCheckType" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.check_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">盘点方式</label>
        <el-select v-model="query.inventoryCheckWay" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.check_way"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">状态</label>
        <el-select v-model="query.status" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.allOrder_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <div style="display: flex;align-items: center;gap: 5px;margin-left: 5px;">
          <rrOperation :crud="crud" />
          <el-button type="primary" size="mini" class="filter-item" @click="goCreateOrder">创建盘点单</el-button>
        </div>
      </div>
      <crudOperation :permission="permission" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <!-- <el-table-column type="selection" width="55" /> -->
        <!-- <el-table-column prop="id" label="主键ID" /> -->
        <el-table-column prop="inventoryCheckCode" label="盘点单号" />
        <el-table-column prop="inventoryCheckType" label="盘点类型">
          <template slot-scope="scope">
            {{ dict.label.check_type[scope.row.inventoryCheckType] }}
          </template>
        </el-table-column>
        <el-table-column prop="inventoryCheckWay" label="盘点方式">
          <template slot-scope="scope">
            {{ dict.label.check_way[scope.row.inventoryCheckWay] }}
          </template>
        </el-table-column>
        <el-table-column prop="inventoryPercent" label="动碰盘点比例">
          <template slot-scope="scope">
            {{ dict.label.check_percent[scope.row.inventoryPercent] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.allOrder_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="note" label="备注" />
        <el-table-column prop="startTime" label="开始时间" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;gap: 5px;">
              <el-button type="text" @click="checkDetail(scope.row)">查看详情</el-button>
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="点击此按钮,将取消该盘点单,是否继续?"
                @confirm="cancelOrder(scope.row)"
              >
                <el-button slot="reference" type="danger" size="mini" :disabled="[2, 3].includes(scope.row.status)" :loading="btn.cancel">取消盘点</el-button>
              </el-popconfirm>
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="点击此按钮,将执行该盘点单,是否继续?"
                @confirm="excuteOrder(scope.row)"
              >
                <el-button slot="reference" type="primary" size="mini" :disabled="[ 2, 3].includes(scope.row.status)" :loading="btn.excute">执行盘点单</el-button>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudInventoryCheckOrder from '@/api/work/checkOrder'
import { cancelOrderAPI, excuteOrderAPI } from '@/api/work/checkOrder'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, inventoryCheckCode: null, inventoryCheckType: null, inventoryCheckWay: null, inventoryPercent: null, status: null, note: null, startTime: null, updateTime: null, createTime: null, createName: null, updateName: null }
export default {
  name: 'CheckOrder',
  components: { pagination, rrOperation, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['check_way', 'check_type', 'check_percent', 'allOrder_status'],
  cruds() {
    return CRUD({ title: 'inventoryCheckOrder', url: 'api/inventoryCheckOrder', idField: 'id', sort: 'id,desc', crudMethod: { ...crudInventoryCheckOrder },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: true
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'inventoryCheckOrder:add', 'otherUser'],
        edit: ['admin', 'inventoryCheckOrder:edit', 'otherUser'],
        del: ['admin', 'inventoryCheckOrder:del', 'otherUser']
      },
      rules: {
        inventoryCheckCode: [
          { required: true, message: '盘点单号不能为空', trigger: 'blur' }
        ],
        inventoryCheckType: [
          { required: true, message: '盘点类型(1-明盤,2-暗盤)不能为空', trigger: 'blur' }
        ],
        inventoryCheckWay: [
          { required: true, message: '盘点方式(1-全盘 2-抽盘(按料号) 3-动碰盘点)不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'inventoryCheckCode', display_name: '盘点单号' },
        { key: 'inventoryCheckType', display_name: '盘点类型(1-明盤,2-暗盤)' },
        { key: 'inventoryCheckWay', display_name: '盘点方式(1-全盘 2-抽盘(按料号) 3-动碰盘点)' }
      ],
      btn: {
        cancel: false,
        excute: false
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    checkDetail(row) {
      this.$router.push(`/storageManagement/checkOrderDetail?inventoryCheckOrderId=${row.id}&inventoryCheckCode=${row.inventoryCheckCode}`)
    },
    async cancelOrder(row) {
      this.btn.cancel = true
      try {
        const res = await cancelOrderAPI(row.inventoryCheckCode)
        if (res.result === 'OK') {
          this.$message.success('取消盘点成功')
          this.crud.refresh()
        } else {
          return this.$message.warning(res.data)
        }
      } finally {
        this.btn.cancel = false
      }
    },
    async excuteOrder(row) {
      this.btn.excute = true
      try {
        const res = await excuteOrderAPI(row.inventoryCheckCode)
        if (res.result === 'OK') {
          this.$message.success('执行盘点单成功')
          this.crud.refresh()
        } else {
          return this.$message.warning(res.data)
        }
      } finally {
        this.btn.excute = false
      }
    },
    goCreateOrder() {
      this.$router.push('/storageManagement/createCheckOrder')
    }
  }
}
</script>

<style scoped>

</style>
